{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3a47ba4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "names=['age','height','weight','gender']\n",
    "dataset=pd.read_csv('gender-data-y.txt',delimiter=',',names=names)\n",
    "print('客户信息数据集')\n",
    "print(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbb91a03",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import preprocessing\n",
    "dataset['height']=dataset['height'].astype(float)\n",
    "dataset['weight']=dataset['weight'].astype(float)\n",
    "le=preprocessing.LabelEncoder()\t            \n",
    "dataset[‘label’]=le.fit_transform(dataset[‘gender’])\t\t\t\t\t             #转换为数值标签\n",
    "print('处理后的客户信息数据集')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f06a4cb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "data=dataset.iloc[range(0,100),range(1,3)].values\t\n",
    "target=dataset.iloc[range(0,100),range(4,5)].values.reshape(1,100)[0]\t\t\t             \n",
    "plt.scatter(data[target==0,0],data[target==0,1],s=60,c='r',marker='o')  \n",
    "plt.scatter(data[target==1,0],data[target==1,1],s=60,c='g',marker='^')\t\t\t         \n",
    "plt.rcParams['font.sans-serif']='Simhei'\n",
    "plt.xlabel('身高/cm')\n",
    "plt.ylabel('体重/kg')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5f3e473",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "x,y=data,target\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=30,random_state=0)\n",
    "depth=np.arange(1,15)\n",
    "err_list=[]\n",
    "for i in depth:\n",
    "    model=DecisionTreeClassifier(criterion='entropy',max_depth=i)\n",
    "     model.fit(x_train,y_train)\n",
    "     pred=model.predict(x_test)\n",
    "     ac=accuracy_score(y_test,pred)\n",
    "     err=1-ac\n",
    "     err_list.append(err)\n",
    "plt.plot(depth,err_list,'ro-')\n",
    "plt.rcParams['font.sans-serif']='Simhei'\n",
    "plt.xlabel('决策树深度')\n",
    "plt.ylabel('预测误差率')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e896707",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "model=DecisionTreeClassifier(criterion='entropy',max_depth=5)\n",
    "model.fit(x_train,y_train)\n",
    "pred=model.predict(x_test)\n",
    "re=classification_report(y_test,pred)\n",
    "print('模型评估报告：')\n",
    "print(re)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09683afb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib.colors import ListedColormap\n",
    "N,M=500,500\t\t          \n",
    "t1=np.linspace(140,195,N)     \n",
    "t2=np.linspace(30,90,M)\t\t        \n",
    "x1,x2=np.meshgrid(t1,t2)\t\t     \n",
    "x_new=np.stack((x1.flat,x2.flat),axis=1)                \n",
    "y_predict=model.predict(x_new)\t             \n",
    "y_hat=y_predict.reshape(x1.shape)\t            \n",
    "iris_cmap=ListedColormap([\"#ACC6C0\",\"#FF8080\"])\t\t\t\t                              #设置分类界面的颜色\n",
    "plt.pcolormesh(x1,x2,y_hat,cmap=iris_cmap)\t\t\t\n",
    "plt.scatter(x[y==0,0],x[y==0,1],s=60,c='r',marker='o')\t\t\t                                #绘制标签为0的样本点\n",
    "plt.scatter(x[y==1,0],x[y==1,1],s=60,c='g',marker='^')\t\t\t\t                             #绘制标签为1的样本点\n",
    "plt.rcParams['font.sans-serif']='Simhei‘\n",
    "plt.xlabel('身高/cm')\n",
    "plt.ylabel('体重/kg')\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
